package com.ruantu.clothing.controller;

import com.ruantu.clothing.entity.*;
import com.ruantu.clothing.service.JgService;
import com.ruantu.clothing.util.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/6/14 0014.
 */
@Controller
@RequestMapping("/jg")
public class JgController {
    private static final Logger logger = LoggerFactory.getLogger(JgController.class);
    @Autowired
    private JgService jgService;

    @RequestMapping("/tree")
    @ResponseBody
    private JsonResponseMessage findTree(Jg jg, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            jg.setId(user.getJgid());
            jg.setJgId(user.getJgid());
            jsonResponseMessage.setRows(jgService.findTree(jg));
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接中断，请刷新重试");
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请联系管理员重试");
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return jsonResponseMessage;
    }

    /**
     * 获得最上级的jgid
     *
     * @param jg
     * @param
     * @return
     */
    @RequestMapping("/tree/all")
    @ResponseBody
    private JsonResponseMessage findTreeById(Jg jg,HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            jg.setJgId(user.getJgid());
            jg.setId("1");
            jsonResponseMessage.setRows(jgService.findTree(jg));
        } catch (NullPointerException e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接中断，请刷新重试");
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器内部错误，请联系管理员重试");
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return jsonResponseMessage;
    }


    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findList(Jg jg, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (user != null) {
            jg.setJgId(user.getJgid());
            jsonResponseMessage.setCode(200);
            jsonResponseMessage.setRows(jgService.findList(jg));
        } else {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接中断，请重新登录进行尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 查询是否存在供应商
     *
     * @return
     */
    @RequestMapping("/find/jgname")
    @ResponseBody
    public JsonResponseMessage findGname(Jg jg) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            jgService.findJgname(jg);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
        }
        return jsonResponseMessage;
    }

    /**
     * 保存供应商
     *
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponseMessage save(Jg jg, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (user != null) {
            try {
                jg.setJgId(user.getJgid());
                jgService.saveJg(jg);
                jsonResponseMessage.setCode(200);
            } catch (MyException e) {
                logger.error(e.getMessage(), e);
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg(e.getMessage());
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg("内部服务器错误，请联系管理员重试");
            }
        } else {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请检查数据重新尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 根据id查询修改反显
     *
     * @return
     */
    @RequestMapping("/find/id")
    @ResponseBody
    public JsonResponseMessage findById(Jg jg, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (user != null) {
            jg.setJgId(user.getJgid());
            try {
                jsonResponseMessage.setCode(200);
                jsonResponseMessage.setRows(jgService.findJgById(jg));
            } catch (MyException e) {
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg(e.getMessage());
                logger.error(e.getMessage(), e);
            } catch (Exception e) {
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg("服务器内部错误，请联系管理员重试");
                logger.error(e.getMessage(), e);
            }
        } else {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接中断，请重新登录进行尝试");
        }
        return jsonResponseMessage;
    }

    /**
     * 删除机构
     *
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public JsonResponseMessage delGongyingshang(Jg jg) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            jgService.delJg(jg);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        }
        return jsonResponseMessage;
    }


    /**
     *根据类型获得分销商客户
     *
     * @return
     */
    @RequestMapping("/selectjgforleixing")
    @ResponseBody
    public JsonResponseMessage selectJGforLeiXing(Jg jg,HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            List<Pifashang> list=jgService.selectJGforLeiXingService(user);
            if(list!=null){
                msg.setCode(200);
                msg.setMsg("获取数据成功!");
                msg.setData(list);
            }else{
                msg.setCode(1);
                msg.setMsg("暂无数据!");
            }

        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            msg.setCode(400);
            msg.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            msg.setCode(400);
            msg.setMsg(e.getMessage());
        }
        return msg;
    }





}
